export interface IProduct {
  id: number
  title: string
  price: number
  inventory: number // 库存
}

const _products: IProduct[] = [
  { id: 1, title: 'iPad 4 Mini', price: 500, inventory: 2 },
  { id: 2, title: 'H&M T-Shirt White', price: 10, inventory: 10 },
  { id: 3, title: 'Charli XCX -Sucker CD', price: 19, inventory: 5 },
]

export const getProducts = async () => {
  // await wait(1000)
  return _products
}
export const buyProducts = async (price: number) => {
  // await wait(1000)
  return !!price
}

/**
 * 封装了Promise版本的定时器
 * @param delay 延迟时间
 * @returns Promise
 */
async function wait(delay: number) {
  return new Promise((resolve) => setTimeout(resolve, delay))
}
